package com.ex.managersystem1024.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ex.managersystem1024.common.Result;
import com.ex.managersystem1024.entity.User;
import com.ex.managersystem1024.exception.ServiceException;
import com.ex.managersystem1024.service.UserService;
import com.ex.managersystem1024.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "User Controller", tags = { "用户相关接口" })
public class UserController {


    @Autowired
    private UserService userService;

    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public Result add(@RequestBody User user){
        return Result.success(userService.add(user));
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        User currentUser = TokenUtils.getCurrentUser();
        if (id.equals(currentUser.getId())) {
            throw new ServiceException("不能删除当前的用户");
        }
        userService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "查询全部用户信息")
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> userList = userService.list(new QueryWrapper<User>().orderByDesc("id"));
        return Result.success(userList);
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.updateById(user);
        return Result.success();
    }







}
